package linkedListStack

import "strings"

type LinkedListStack[E any] struct {
	list *LinkedList[E]
}

func MakeLinkedListStack[E any]() *LinkedListStack[E] {
	return &LinkedListStack[E]{
		list: MakeLinkedList[E](),
	}
}

// GetSize 查看栈元素数量
func (l *LinkedListStack[E]) GetSize() int {
	return l.list.GetSize()
}

// Pop 出栈
func (l *LinkedListStack[E]) Pop() (E, error) {
	return l.list.RemoveFirst()
}

// Push 压栈
func (l *LinkedListStack[E]) Push(e E) error {
	return l.list.AddFirst(e)
}

// Peek 查看栈顶元素
func (l *LinkedListStack[E]) Peek() (E, error) {
	return l.list.GetFirst()
}

//IsEmpty 栈是否为空
func (l *LinkedListStack[E]) IsEmpty() bool {
	return l.list.IsEmpty()
}

func (l *LinkedListStack[E]) String() string {
	sb := strings.Builder{}
	sb.WriteString("Stack: top ")
	sb.WriteString(l.list.String())
	return sb.String()
}
